﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;

using System.Collections.Specialized;
using NUnit.Framework;
using EasyNet.Solr;
using EasyNet.Solr.Commons;
using EasyNet.Solr.Commons.Params;
using EasyNet.Solr.Impl;

namespace SolrExample.Tests.Temp
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestFixture]
    public class SolrOperation_Test2
    {
        [TestFixtureSetUp]
        public void InitializeAllTests()
        {

        }

        [Test]
        public void Test1()
        {
            string solrUrl = "http://127.0.0.1:8080/solr-HDM/HK";
            //string solrUrl = "http://10.1.0.95:8080/solr-HD1/HK";

            //ISolrConnection con = new SolrReadonlyConnection<NamedList>();

            //ISolrConnection con = new SolrConnection(solrUrl);
            string searchQuery = "name:name";

            Console.WriteLine("HK".ToUpperInvariant());
            ISolrReadonlyConnection<NamedList> con = new SolrReadonlyConnection<NamedList>() { ServerUrl = solrUrl };
            ISolrQueryOperations<NamedList> solrCore = new SolrQueryOperations<NamedList>(con);
            SolrQuery solrQuery = new SolrQuery(searchQuery);
            
            var solrQueryOption = new Dictionary<string, ICollection<string>>();
            solrQueryOption[CommonParams.WT] = new string[] { "javabin" };
            solrQueryOption[CommonParams.FL] = new string[]{"id", "company"};
            solrQueryOption[CommonParams.START] = new string[] { "0" };
            solrQueryOption[CommonParams.ROWS] = new string[] { "100" };
            solrQueryOption[CommonParams.SORT] = new string[] { "id desc,company desc" };

            solrQueryOption[HighlightParams.HIGHLIGHT] = new string[] { "true" };
            solrQueryOption[HighlightParams.FIELDS] = new string[] { "id" };
                
            NameValueCollection options = new NameValueCollection();
            options.Add(CommonParams.START, "0");
            options.Add(CommonParams.ROWS, "50");

            NamedList searchResult = solrCore.Query(solrQuery, solrQueryOption);

            //解析返回头信息
            var binaryResponseHeaderParser = new BinaryResponseHeaderParser();
            var responseHeader = binaryResponseHeaderParser.Parse(searchResult);
            Console.WriteLine("Status:{0} QTime:{1}", responseHeader.Status, responseHeader.QTime);


            //解析查询结果
            ISolrResponseParser<NamedList, QueryResults<SolrSearchResultRecord>> binaryQueryResultsParser = new BinaryQueryResultsParser<SolrSearchResultRecord>(new SolrObjectDeSerializer());
            QueryResults<SolrSearchResultRecord> queryResult = binaryQueryResultsParser.Parse(searchResult);
            Console.WriteLine("Total:{0} Result:{1}", queryResult.NumFound, queryResult.Count);

            //解析高亮
            IDictionary<string, IDictionary<string, IList<string>>> hightlightResult;
            ISolrResponseParser<NamedList, IDictionary<string, IDictionary<string, IList<string>>>> binaryHighlightingParser = new BinaryHighlightingParser();
            hightlightResult = binaryHighlightingParser.Parse(searchResult);

            IDictionary<string, IList<FacetField>> facetResult;
            ISolrResponseParser<NamedList, IDictionary<string, IList<FacetField>>> binaryFacetFieldsParser = new BinaryFacetFieldsParser();
            facetResult = binaryFacetFieldsParser.Parse(searchResult);

        }

        [Test]
        public void Test2() {
        }

        [TestFixtureTearDown]
        public void CleanupAllTests() { }

    }

    public class SolrSearchResultRecord
    {
        public long Id { get; set; }
        public string Name { get; set; }
    }
}
